package chapter13;

import lombok.Data;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

@Data
public class ExecuteDDL {
    private String url;
    private String driver;
    private String username;
    private String password;

    public void initParam(String paramFile) throws Exception{
        Properties props = new Properties();
        props.load(new FileInputStream(paramFile));
        driver = props.getProperty("driver");
        url = props.getProperty("url");
        username = props.getProperty("username");
        password = props.getProperty("password");
    }

    public void createTable(String sql) throws Exception {
        Class.forName(driver);
        try(Connection connection = DriverManager.getConnection(url, username, password);
            Statement statement = connection.createStatement()){
            statement.executeUpdate(sql);
        }
    }

    public static void main(String[] args) throws Exception {
        ExecuteDDL ddl = new ExecuteDDL();
        ddl.initParam("src/main/resources/db.properties");
        ddl.createTable("create table jdbc_test "
        + "(jdbc_id int auto_increment primary key, "
        + "jdbc_name varchar(255),"
        + "jdbc_desc text);");
        System.out.println("建表成功.");
    }
}
